package com.dcbeida.activity.stockdetail;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.dcbeida.MyApplication;
import com.dcbeida.bean.StockDetail;
import com.dcbeida.callback.DialogCallback;
import com.dcbeida.model.ModelUtils;
import com.dcbeida.utils.JsonGet;

import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by 150410 on 2017/10/18.
 */

public class StockDetailPersentr implements StockDetailContract.Persent {
    private final ModelUtils model;
    private StockDetailContract.View view;
    private Activity mActivity;
    //取消dialog 取消请求
    private DialogInterface.OnCancelListener listener = new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            dialog.dismiss();
        }
    };

    public StockDetailPersentr(StockDetailContract.View view, Activity activity) {
        this.view = view;
        this.mActivity = activity;
        model = ModelUtils.newInstance();
    }

    @Override
    public void start() {

    }

    @Override
    public void getList(String isall, int pageindex, int pagesize, String batch, int drugId) {
        getList(isall, pageindex, pagesize, batch, drugId,true);
    }

    @Override
    public void getList(String isall, int pageindex, int pagesize, String batch, int drugId,boolean isShow) {
        JsonGet jg;
        if (!TextUtils.isEmpty(batch)) {
            jg = new JsonGet().putopt("uid", MyApplication.user.getUid())
                    .putopt("shopid", MyApplication.user.getShopid())
                    .putopt("isall", isall)
                    .putopt("pageindex", pageindex)
                    .putopt("pagesize", pagesize)
                    .putopt("batch", batch)
                    .putopt("justbatch", 0);
        } else {
            jg = new JsonGet().putopt("uid", MyApplication.user.getUid())
                    .putopt("shopid", MyApplication.user.getShopid())
                    //.putopt("isall", isall)
                    .putopt("drugid", drugId)
                    .putopt("pageindex", pageindex)
                    .putopt("pagesize", pagesize);
        }

        String json = jg.tostring();

        model.getDate("getstocklistv2", json, new GetListCallBack(mActivity, StockDetail.class, listener,isShow));
    }

    public class GetListCallBack<T> extends DialogCallback<T> {
        public GetListCallBack(Activity activity, Type type, DialogInterface.OnCancelListener lis) {
            super(activity, type, lis);
        }
        public GetListCallBack(Activity activity, Type type, DialogInterface.OnCancelListener lis, boolean isShow) {
            super(activity, type, lis,isShow);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                if (((StockDetail) data).getList().size() > 0) {
                    view.showView(true);
                    view.showCount(((StockDetail) data).getStock());
                    view.showList(((StockDetail) data).getList());
                    view.showData(((StockDetail) data));
                } else {
                    view.showView(false);
                    view.showData(((StockDetail) data));
                }
            } else {
                view.showView(false);
            }
        }

        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showView(false);
            view.showTelTip("数据未请求成功,请检查网络...");
        }
    }
}
